package com.example.rtmppush;

import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;

import android.content.Context;
import android.content.Intent;
import android.media.projection.MediaProjection;
import android.media.projection.MediaProjectionManager;
import android.os.Build;
import android.os.Bundle;
import android.view.View;

public class MainActivity extends AppCompatActivity {

    MediaProjectionManager  mediaProjectionManager;

    static {
       System.loadLibrary("rtmppush");
    }
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
    }

    public void startLive(View view) {

        mediaProjectionManager = (MediaProjectionManager) getSystemService(Context.MEDIA_PROJECTION_SERVICE);
        Intent screenCaptureIntent = mediaProjectionManager.createScreenCaptureIntent();
        startActivityForResult(screenCaptureIntent,1);
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (resultCode == RESULT_OK && requestCode ==1) {


            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
                //
                Intent intent =  new Intent(this,ScreenPushService.class);
                intent.putExtra("resultCode",resultCode);
                intent.putExtra("data",data);
                startForegroundService(intent);
            }else {
                MediaProjectionManager mediaProjectionManager = (MediaProjectionManager) getSystemService(MEDIA_PROJECTION_SERVICE);
                MediaProjection mediaProjection = mediaProjectionManager.getMediaProjection(resultCode, data);
                H264Encoder h264Encoder = new H264Encoder(mediaProjection,this.getExternalFilesDir("screenPush").getAbsolutePath());
                h264Encoder.start();
            }



        }
    }
}